package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.pojos.DBuddhism;
import com.jilefojing.beans.pojos.base.BasePo;
import com.jilefojing.beans.pojos.enhanced.EnhancedDBuddhism;
import com.jilefojing.beans.query.DBuddhismQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IDBuddhismDao;
import com.jilefojing.service.IDBuddhismService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;
import com.jilefojing.util.QiniuUtil;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年10月06日 23时59分44秒
 */
@Service
public class DBuddhismService extends BaseService<DBuddhism> implements
		IDBuddhismService {
	@Autowired
	private IDBuddhismDao dBuddhismDao;

	@Override
	public List<EnhancedDBuddhism> getEnhanceds(List<Long> ids,
			UserContext userContext) {
		List<DBuddhism> dBuddhisms = dBuddhismDao.getListByIds(ids);
		if (!Detect.notEmpty(dBuddhisms)) {
			return null;
		}
		List<EnhancedDBuddhism> enhancedDBuddhisms = this
				.convent2Enhanceds(dBuddhisms);
		this.appendEnhancedCommons(enhancedDBuddhisms, userContext);

		return enhancedDBuddhisms;
	}

	@Override
	public Pagination<EnhancedDBuddhism> getPagination(
			DBuddhismQuery dBuddhismQuery,
			Pagination<EnhancedDBuddhism> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<DBuddhism> dBuddhismPagination = dBuddhismDao.getPagination(
				dBuddhismQuery, pagination.copy());
		pagination.setCount(dBuddhismPagination.getCount());
		if (!Detect.notEmpty(dBuddhismPagination.getItems())) {
			return pagination;
		}
		pagination.setItems(this.convent2Enhanceds(dBuddhismPagination
				.getItems()));
		return pagination;
	}

	@Override
	public List<EnhancedDBuddhism> convent2Enhanceds(List<? extends BasePo> pos) {
		@SuppressWarnings("unchecked")
		List<DBuddhism> dBuddhisms = (List<DBuddhism>) pos;

		List<EnhancedDBuddhism> enhancedDBuddhisms = new ArrayList<EnhancedDBuddhism>();
		for (DBuddhism _DBuddhism : dBuddhisms) {
			if (Detect.notEmpty(_DBuddhism.getCoverUrl())
					&& !_DBuddhism.getCoverUrl().startsWith(SCHEMA)) {
				_DBuddhism.setCoverUrl(QiniuUtil.getImagesDomain() + "/"
						+ _DBuddhism.getCoverUrl());
			}
			enhancedDBuddhisms.add(new EnhancedDBuddhism(_DBuddhism));
		}
		return enhancedDBuddhisms;
	}
}
